/*
 * @lc app=leetcode.cn id=811 lang=javascript
 *
 * [811] 子域名访问计数
 */

// @lc code=start
/**
 * @param {string[]} cpdomains
 * @return {string[]}
 */
var subdomainVisits = function (cpdomains) {
  const map = {};

  cpdomains
    .map(cpdomain => {
      return cpdomain.split(' ');
    })
    .forEach(([num, domain]) => {
      const domainArr = domain.split('.')
      for (let i = 0; i < domainArr.length; i++) {
        const str = domainArr.slice(i).join('.');
        if (map[str]) {
          map[str] += +num;
        } else {
          map[str] = +num
        }
      }
    });
  return Object
    .entries(map)
    .map(([key, value]) => `${value} ${key}`);
};
// @lc code=end

